package clues.bihu.cluesaccident.jpush;

import android.app.Activity;
import android.content.Context;

import java.io.IOException;

import clues.bihu.cluesaccident.ClueBaseApp;
import clues.bihu.cluesaccident.JPushUtil;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class JpushPlugin implements MethodChannel.MethodCallHandler, EventChannel.StreamHandler {

    private Activity activity;
    private static final String CHANNEL_METHED = "plugins.flutter.io/jpush_method";
    private static final String CHANNEL_EVENT = "plugins.flutter.io/jpush_event";

    private static EventChannel.EventSink sink;

    public static EventChannel.EventSink getSink() {
        return sink;
    }


    private JpushPlugin(Activity context) {
        activity = context;
    }

    public static void registerWith(PluginRegistry.Registrar registrar) {
        MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_METHED);
        EventChannel eventChannel = new EventChannel(registrar.messenger(), CHANNEL_EVENT);
        JpushPlugin jpushPlugin = new JpushPlugin(registrar.activity());
        channel.setMethodCallHandler(jpushPlugin);
        eventChannel.setStreamHandler(jpushPlugin);
    }


    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        try {
            switch (methodCall.method) {
                case "addTag_ID": {
                    JPushUtil.Companion.addTag_ID(ClueBaseApp.Companion.getAppContext());
                    break;
                }
                case "removeTag_ID": {
                    JPushUtil.Companion.removeTag_ID(ClueBaseApp.Companion.getAppContext());
                    break;
                }
            }

        } catch (Exception e) {
            result.error("IOException encountered", methodCall.method, e);
        }
    }

    @Override
    public void onListen(Object o, EventChannel.EventSink eventSink) {
        if (sink == null) {
            sink = eventSink;
        }

    }

    @Override
    public void onCancel(Object o) {

    }
}
